from multiprocessing import Process, Lock
import time

lock = Lock()  # 创建锁对象


def task1():
    for i in range(5):
        with lock:
            print(f"任务1执⾏中... {i}")
            time.sleep(0.5)


def task2():
    for i in range(5):
        with lock:
            print(f"任务2执⾏中... {i}")
            time.sleep(0.5)


if __name__ == '__main__':
    process1 = Process(target=task1)
    process2 = Process(target=task2)
    process1.start()
    process2.start()
    process1.join()
    process2.join()
